package com.sdsy.service.impl;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sdsy.dao.VipWalletAddrDao;
import com.sdsy.dao.VipWalletAddrMidDao;
import com.sdsy.domain.VipWalletAddrDO;
import com.sdsy.domain.VipWalletAddrMidDO;
import com.sdsy.exception.WalletException;
import com.sdsy.service.VipWalletAddrMidService;
import com.sdsy.util.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员钱包地址管理(VipWalletAddrMid)表服务实现类
 *
 * @author yxc
 * @since 2018-09-04 10:44:21
 */
@Service
public class VipWalletAddrMidServiceImpl extends ServiceImpl<VipWalletAddrMidDao, VipWalletAddrMidDO> implements VipWalletAddrMidService {


    @Autowired
    private VipWalletAddrMidDao vipWalletAddrMidDao;

    @Autowired
    private VipWalletAddrDao vipWalletAddrDao;

    /**
     * 更新FO_VIP_WALLET_ADDR
     * FO_VIP_WALLET_ADDR_MID
     *
     * @param vipNo
     * @return
     */
    @Override
    @Transactional(readOnly = false)
    public boolean generateWallet(String vipNo) {

        final int status = 0;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("VIP_NO", vipNo);
        List<VipWalletAddrDO> list = vipWalletAddrDao.selectByMap(map);

        EntityWrapper<VipWalletAddrMidDO> ew = new EntityWrapper<VipWalletAddrMidDO>();
        ew.where("status={0}", status);
        List<VipWalletAddrMidDO> qb_list = vipWalletAddrMidDao.selectList(ew);

        if (!SysUtil.isNull(list)) {
            VipWalletAddrDO entity = list.get(0);
            if (!SysUtil.isNull(qb_list) && qb_list.size() > 0) {
                VipWalletAddrMidDO vo = qb_list.get(0);
                entity.setWalletAddr(vo.getWalletAddr());
                Integer count = vipWalletAddrDao.updateById(entity);
                vo.setStatus(1l);
                Integer num = vipWalletAddrMidDao.updateById(vo);
                if (count > 0 && num > 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                throw new WalletException(HttpStatus.HTTP_INTERNAL_ERROR, "列表中没有可用的钱包地址");
            }
        } else {
            throw new WalletException(HttpStatus.HTTP_INTERNAL_ERROR, "地址列表中不存在对应的用户信息");
        }


    }
}